

/********************************************************************/
/*                                                                  */ 
/* Export variables used for regressions to STATA                   */
/*                                                                  */ 
/********************************************************************/




libname common 'G:\Data\Workdata\707677\Common\01Data\01SAS';

libname external 'G:\Data\Rawdata\707677\Eksterne data\';




/*****************************/
/* Load cross-sectional data */
/*****************************/


/* IVs */


data iv;

set common.ivs;

keep pnr court obs_lawyer lawyerid;

run;




/* Base data */


data list;

set common.list;

keep pnr schooling;

run;




/* More base data */



data mlist;

set common.panel;

keep pnr granted pyear male age0;

run;



proc sort data=mlist nodupkey;

by pnr;

run;




/* Data on repayment terms */


data repay;

set external.reduced_dst;

rename dividend=div_short;

run;



data key;

set external.population;

keep person_id pnr;

run;



proc sql;

create table repay as select *

from repay as a left join

key as b on 

a.person_id=b.person_id;

quit;




data repay;

set repay;

drop person_id;

run;




/* Merge cross-sectionial data */



proc sql;

create table list as select *

from list as a left join

mlist as b on 

a.pnr=b.pnr;

quit;




proc sql;

create table list as select *

from list as a left join

iv as b on 

a.pnr=b.pnr;

quit;




proc sql;

create table list as select *

from list as a left join

repay as b on 

a.pnr=b.pnr;

quit;




proc datasets library=work nolist;

modify list;

attrib _all_ label='';

quit;





proc datasets library=work nolist;

delete iv mlist repay key;

quit;





/*****************************************/
/* Add data on outcomes before treatment */
/*****************************************/



data pre;

set common.panel;

keep pnr time pstill

AKTIV_real

PASSIV_real

FORMUE_real

erhvervsindk_real

employed_pstill

unemployed_pstill

out_of_lf_pstill

kontant_real

social

legmarried

house

ie_type

antpersf

edu_type

edu_type_extended

DISPON_NY_real

ANTBOERNF

TIMELON_real

antpersf_d;
 
if time<0;

run;




data before;

set pre;

keep pnr;

if time=-4;

run;





%macro prelevel(number);



data before&number.;

set pre;

if time=-&number.;

run;



data before&number.;

set before&number.;

rename AKTIV_real=AKTIV_M&number.;

rename PASSIV_real=PASSIV_M&number.;

rename FORMUE_real=FORMUE_M&number.;

rename erhvervsindk_real=erhvervsindk_M&number.;

rename employed_pstill=employed_pstill_M&number.;

rename unemployed_pstill=unemployed_pstill_M&number.;

rename out_of_lf_pstill=out_of_lf_pstill_M&number.;

rename kontant_real=kontant_M&number.;

rename social=social_M&number.;

rename legmarried=legmarried_M&number.;

rename house=house_M&number.;

rename TIMELON_real=TIMELON_real_M&number.;

rename ie_type=ie_type_M&number.;

rename antpersf=antpersf_M&number.;

rename DISPON_NY_real=DISPON_NY_real_M&number.;

rename edu_type=edu_type_M&number.;

rename edu_type_extended=edu_type_extended_M&number.;

rename ANTBOERNF=ANTBOERNF_M&number.;

rename antpersf_d=antpersf_d_M&number.;

drop time;

run;




proc sql;

create table before as select *

from before as a left join

before&number. as b on

a.pnr=b.pnr;

quit;



proc datasets library=work nolist;

delete before&number.;

quit;




%mend;



%prelevel(4);

%prelevel(3);

%prelevel(2);

%prelevel(1);




proc sql;

create table list as select *

from list as a left join

before as b on

a.pnr=b.pnr;

quit;





/************************************/
/* Retrieve panel data on outcomes  */
/************************************/ 



data panel;

set common.panel;

run;




data panel;

set panel;

keep pnr age time aar kom

TIMELON_real

AKTIV_real

PASSIV_real

FORMUE_real

KOEJD_real

BANKGAELD_real

mortgage_real

erhvervsindk_real

house

employed_pstill

unemployed_pstill

out_of_lf_pstill

tilbtot_real

disability

kontant_real

social

DISPON_NY_real

netovskud_real

legmarried;   


if time in (-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

run;




data panel;

set panel;

age2=age*age;

run;




proc sql;

create table panel as select *

from panel as a left join

list as b on

a.pnr=b.pnr;

quit;



proc sort data=panel;

by pnr time;

run;







/************************/
/* Export data to STATA */
/************************/



proc sort data=panel nodupkey;

by pnr aar;

run;





proc export data=panel outfile= "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv_exclusion.dta" replace; 

run;





proc datasets library=work kill nolist;

quit;


